import kpsImageCutter from '@/components/ksp-image-cutter/ksp-image-cutter.vue'
export default {
  components: {
    kpsImageCutter
  },
  data() {
    return {
      perList: {
        headportrait: '',
        sex: 0, //0、未设置、1、男 2、女
        birthday: '',
        nickname: '',
      }, //个人信息
      sexpopup: false, //性别弹框
      sexlist: [{
        name: '男',
        id: 1
      }, {
        name: '女',
        id: 2
      }],
      params: {
        year: true,
        month: true,
        day: true,
        hour: false,
        minute: false,
        second: false
      },
      timeshow: false,
      personali: {
        headportrait: '',
        whatname: '',
        gender: 0,
        birthday: '',
      },
      addressshow: false, //收货地址是否点击

      success: false, //提交成功
      selectLog: true, //选择图片为true
      logoImg: '', //图片裁剪
      path: '', //图片裁剪路径
      showPage: false

    }
  },
  onShow() {
    this.addressshow = false
    this.GetUserInfomation()
  },
  // 监听返回
  onBackPress(e) {
    if (this.perList.headportrait !== this.headportrait) {
      this.success = true
      return true
    }
    if (this.perList.nickname !== this.whatname) {
      this.success = true
      return true
    }
    if (this.perList.sex !== this.gender) {
      this.success = true
      return true
    }
    if (this.perList.birthday !== this.birthday) {
      this.success = true
      return true
    }
  },
  //分享给微信好友
  onShareAppMessage: function () {
    let url='/pages_userModule/distriBution/distriBution?code='+ this.perList.invitecode
    return {
      title: '在吗？100%纯羊毛等你来薅哟~',
      imageUrl: 'https://files.yzsheng.com/client/weixin/fuli.jpg',
      path: url
    }
  },
  methods: {
    // 保存，返回首页 取消保存
    shenclick(type) {
      if (this.addressshow) {
        if (type) {
          this.addressshow = true
          this.UpdateUserInfomation()
        } else {
          this.addressshow = false
          this.success = false
          uni.navigateTo({
            url: '/pages_userModule/my_address/my_address'
          })
        }
      } else {
        this.success = false
        if (type) {
          this.UpdateUserInfomation()
        } else {
          uni.switchTab({
            url: '/pages/me/me'
          })
        }
      }
    },
    //个人信息
    async GetUserInfomation() {
      try {
        const res = await this.$api.user.GetUserInfomation({})
        this.perList = res.data
        this.showPage = true
        this.headportrait = res.data.headportrait
        this.whatname = res.data.nickname
        this.gender = res.data.sex
        this.birthday = res.data.birthday
      } catch (err) {}
    },
    //跳转详情
    ieClick(type) {
      uni.navigateTo({
        url: '/pages_userModule/incomeExpenditurDetails/incomeExpenditurDetails?ieid=' + type.moneyId
      })
    },
    //选中性别
    sexclicks(type) {
      this.perList.sex = type.id
      this.sexpopup = false
    },
    timeclick(data) {
      this.perList.birthday = data.year + '-' + data.month + '-' + data.day
    },
    //是否认证
    arrroveClick(type) {
      if (type) {
        uni.showToast({
          title: '已认证',
          icon: 'none'
        })
      } else {
        uni.navigateTo({
          url: '/pages_commodity/idCarsAuthentication/idCarsAuthentication?typeid=110'
        })
      }
    },
    //保存信息
    preserClick() {
      this.UpdateUserInfomation()
    },
    //修改个人信息
    async UpdateUserInfomation(type) {
      try {
        const res = await this.$api.user.UpdateUserInfomation({
          headportrait: this.perList.headportrait,
          nickname: this.$u.trim(this.perList.nickname),
          sex: this.perList.sex,
          birthday: this.perList.birthday
        })
        if (res.code === 200) {
          this.success = false
          this.GetUserInfomation()
          if (this.addressshow) {
            uni.navigateTo({
              url: '/pages_userModule/my_address/my_address'
            })
            setTimeout(() => {
              uni.showToast({
                title: '保存成功',
                icon: 'none'
              })
            }, 100)

          } else {
            if (type !== 110) {
              uni.switchTab({
                url: '/pages/me/me'
              })
              setTimeout(() => {
                uni.showToast({
                  title: '保存成功',
                  icon: 'none'
                })
              }, 100)
            } else {
              uni.hideLoading()
              setTimeout(() => {
                uni.showToast({
                  title: '头像保存成功',
                  icon: 'none'
                })
              }, 100)
            }

          }
        }
      } catch (err) {}
    },
    //地址
    addressClick() {
      this.addressshow = true
      if (this.perList.headportrait !== this.headportrait) {
        this.success = true
        return true
      }
      if (this.perList.nickname !== this.whatname) {
        this.success = true
        return true
      }
      if (this.perList.sex !== this.gender) {
        this.success = true
        return true
      }
      if (this.perList.birthday !== this.birthday) {
        this.success = true
        return true
      }
      uni.navigateTo({
        url: '/pages_userModule/my_address/my_address'
      })
    },
    // 复制到剪切板 h5不可复制
    copyCode(value) {
      uni.setClipboardData({
        data: value,
        success: function() {
          uni.showToast({
            title: '复制成功',
            icon: 'none'
          })
        }
      })
    },
    // 把图片保存到本地 h5不可保存
    saveImgLocal(value) {
      uni.downloadFile({
        url: value,
        success: res => {
          if (res.statusCode === 200) {
            uni.saveImageToPhotosAlbum({
              filePath: res.tempFilePath,
              success: function() {
                uni.showToast({
                  title: '保存成功',
                  icon: 'none'
                })
              },
              fail: function() {
                // #ifndef MP-WEIXIN
                uni.showToast({
                  title: '保存失败，请稍后重试',
                  icon: 'none'
                })
                //  #endif
                // #ifdef MP-WEIXIN
                uni.getSetting({
                  success: res => {
                    if (!res.authSetting['scope.writePhotosAlbum']) {
                      wx.showModal({
                        content: '检测到您没打开保存图片权限，是否去设置打开？',
                        success: function (res) {
                          if (res.confirm) {
                            wx.openSetting()
                          }
                        }
                      })
                    }
                  }
                })
                //  #endif
              }
            })
          }
        }
      })
    },
    //选择图片
    uploadImage() {

      uni.chooseImage({
        count: 1,
        sourceType: ['album', 'camera'],
        sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
        success: result => {
          this.logoImg = result.tempFilePaths[0]
          this.selectLog = false
          // uni.showLoading({
          //   title: '图片上传中~',
          // })
          // this.$api.files.uploadImg({
          //   filePath: result.tempFilePaths[0], // 图片流
          // }).then(res => {
          //   console.log(res)
          //   var urls = res.baseUrl + res.url
          //   this.perList.headportrait = urls
          //   this.UpdateUserInfomation(110)
          //   // console.log(this.perList.headportrait)
          //   uni.hideLoading()
          // })
        },
        error: function(e) {
          this.selectLog = true
        }
      })

    },
    //分享微信好友
    goPartner(type) {
      //#ifdef APP-PLUS
      let code = this.perList.invitecode
      uni.share({
        provider: 'weixin',
        scene: type,
        type: 0,
        summary: '在吗？100%纯羊毛等你来薅哟~',
        title: '送1000元购车抵用券+39元免费洗车券！点击领取吧',
        href: 'https://files.yzsheng.com/shareH5/Qmc_Client_H5/index.html?code=' + code, //这地址太长了，就省略了
        imageUrl: 'https://files.yzsheng.com/client/me/img/logo.png',
        success: function(res) {
          console.log('success:' + JSON.stringify(res))
        },
        fail: function(err) {
          console.log('fail:' + JSON.stringify(err))
          uni.showToast({
            title: '分享失败'
          })
        }
      })
      //#endif
    },
    onok(ev) {
      this.path = ev.path
      // this.logoUrl=ev.path
      // this.url=ev.path
      console.log(ev.path)
      this.$api.files

        .uploadImg({
          filePath: this.path,
        })
        .then(res => {
          console.log(res)
          this.logoUrl = res.baseUrl + res.url

          this.logoImg = ''
          this.perList.headportrait = this.logoUrl
          uni.showLoading({
            title: '加载中',
            mask: true
          })
          this.UpdateUserInfomation(110)
        })

        .catch(err => {
          console.log(err)
        })
      this.selectLog = true
    },

    oncancle() {
      // url设置为空，隐藏控件
      this.logoImg = ''
      this.selectLog = true
    },
  }
}
